﻿using CefSharp;

namespace AwesomeChromeFormUI.Handlers
{
    public class DefaultLifeSpanHandler : ILifeSpanHandler
    {
        /// <summary>
        /// 在此处处理关闭事件
        /// </summary>
        /// <param name="chromiumWebBrowser"></param>
        /// <param name="browser"></param>
        /// <returns></returns>
        public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            // 返回 false 表示允许关闭窗口
            return false;
        }

        /// <summary>
        /// 在此处处理新窗口打开事件
        /// </summary>
        /// <param name="chromiumWebBrowser"></param>
        /// <param name="browser"></param>
        /// <param name="frame"></param>
        /// <param name="targetUrl"></param>
        /// <param name="targetFrameName"></param>
        /// <param name="targetDisposition"></param>
        /// <param name="userGesture"></param>
        /// <param name="popupFeatures"></param>
        /// <param name="windowInfo"></param>
        /// <param name="browserSettings"></param>
        /// <param name="noJavascriptAccess"></param>
        /// <param name="newBrowser"></param>
        /// <returns></returns>
        public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            // 设置 newBrowser 为 null，阻止弹出新窗口
            newBrowser = null;
            // 在当前窗口中加载目标 URL
            chromiumWebBrowser.Load(targetUrl);
            // 返回 true 表示已处理该事件
            return true;
        }

        public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            //throw new System.NotImplementedException();
        }

        public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
        {
            //throw new System.NotImplementedException();
        }


    }
}
